package json;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class JsonReadWrite {
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        File jsonFile = new File(fileName);
        if (!jsonFile.exists())return "-1";
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void writeJsonFile(String fileName,String jsonStr) {
        File jsonFile = new File(fileName);
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(jsonFile));
            bw.write(jsonStr);
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
